﻿using System;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.Sugar;
using Medianamik.UI.Controls;
using Medianamik.UI.Web.TypeList.Data;

namespace Medianamik.UI.Web.TypeList
{
    public partial class Edit : BaseModuleEditionPage<NodeWrapper>
    {
        private string _returnUrl;
        protected override string ReturnURL
        {
            get { return _returnUrl ?? (_returnUrl = GetReturnUrl()); }
        }
        private string GetReturnUrl()
        {
            var returnUrl = base.ReturnURL;
            if (CurrentTypeId != null)
            {
                if (!returnUrl.Contains(CurrentTypeId.ToString()))
                {
                    returnUrl = returnUrl.RemoveParam(DefaultParamNames.TypeID)
                                        .AddParam(DefaultParamNames.TypeID, CurrentNode.NodeTypeID);
                }
            }
            else
                returnUrl = returnUrl.AddParam(DefaultParamNames.TypeID, CurrentNode.NodeTypeID);

            return returnUrl;
        }

        public override LinkButton SaveButton
        {
            get { return SaveLinkButton; }
        }

        public override MedianamikFormTemplate MedianamikFormTemplate
        {
            get { return Fields; }
        }

        protected override string TypeIdParamName
        {
            get { return DefaultParamNames.TypeID; }
        }

        protected override string ParentNodeIdParamName
        {
            get { return DefaultParamNames.ParentNodeID; }
        }

        private Guid? _currentTypeId;
        private Guid? CurrentTypeId
        {
            get { return _currentTypeId ?? (_currentTypeId = GetCurrentTypeId()); }
        }
        private Guid? GetCurrentTypeId()
        {
            return GetQueryStringValue<Guid?>(DefaultParamNames.TypeID).ValidateGuid();
        }
    }
}
